package com.ifast.asset.controller;


import java.util.Arrays;
import java.util.List;

import com.ifast.asset.domain.TaskrecordDO;
import com.ifast.asset.domain.Webgroup;
import com.ifast.asset.domain.WebgroupDO;
import com.ifast.asset.quartz.service.JobService;
import com.ifast.asset.service.TaskrecordService;
import com.ifast.asset.service.WebgroupService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ifast.common.annotation.Log;
import com.ifast.common.base.BaseController;
import com.ifast.common.utils.Result;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2021-08-05 14:19:16 | Aron</small>
 */
@Controller
@RequestMapping("/wxmp/webgroup")
public class WebgroupController extends BaseController {
	@Autowired
	private WebgroupService webgroupService;
	@Autowired
	TaskrecordService taskrecordService;
	@Autowired
	private JobService jobService;

	@GetMapping()
	@RequiresPermissions("wxmp:webgroup:webgroup")
	String Webgroup(){
	    return "wxmp/webgroup/webgroup";
	}
	
	@ResponseBody
	@GetMapping("/list")
	//@RequiresPermissions("wxmp:webgroup:webgroup")
	Result<Page<WebgroupDO>> list(Webgroup webgroup){
        WebgroupDO webgroupDTO = webgroup.getWebgroupDO();
        Wrapper<WebgroupDO> wrapper = new EntityWrapper<>(webgroupDTO).orderBy("id", false);
        Page<WebgroupDO> page = webgroupService.selectPage(getPage(WebgroupDO.class,webgroup.pageNum,webgroup.getPageSize()), wrapper);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("wxmp:webgroup:add")
	String add(){
	    return "wxmp/webgroup/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("wxmp:webgroup:edit")
	String edit(@PathVariable("id") Integer id,Model model){
		WebgroupDO webgroup = webgroupService.selectById(id);
		model.addAttribute("webgroup", webgroup);
	    return "wxmp/webgroup/edit";
	}
	
	@Log("添加")
	@ResponseBody
	@PostMapping("/save")
	//@RequiresPermissions("wxmp:webgroup:add")
	public Result<String> save(@RequestBody WebgroupDO webgroup){
		// 是否已经存在  该资产组名称
		Wrapper wrapper = new EntityWrapper();
		wrapper.eq("name",webgroup.getName());
		List<WebgroupDO> lists = webgroupService.selectList(wrapper);
		if (lists.size()>0){
			return Result.ok("该名称已存在");
		}
		webgroupService.insert(webgroup);
        return Result.ok();
	}
	
	@Log("修改")
	@ResponseBody
	@RequestMapping("/update")
	//@RequiresPermissions("wxmp:webgroup:edit")
	public Result<String>  update(@RequestBody WebgroupDO webgroup){
		boolean update = webgroupService.updateById(webgroup);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wxmp:webgroup:remove")
	public Result<String>  remove( Integer id){


		webgroupService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除")
	@PostMapping( "/batchRemove")
	@ResponseBody
	//@RequiresPermissions("wxmp:webgroup:batchRemove")
	public Result<String>  remove(@RequestBody Integer[] ids){
		//判断是否含有该资产组的 任务
		for (int j = 0 ; j < ids.length; j++){
			Wrapper wrapper = new EntityWrapper();
			wrapper.eq("groupid",ids[j]);
			List<TaskrecordDO > lists = taskrecordService.selectList(wrapper);
			for (int i = 0 ; i < lists.size() ; i++){
				TaskrecordDO taskrecordDO = lists.get(i);
				String str =taskrecordDO.getGroupname()+"（已被删除）";
				taskrecordDO.setGroupname(str);
				taskrecordService.updateById(taskrecordDO);
				if ("1".equals(taskrecordDO.getType())){
					jobService.deleteJob(taskrecordDO.getTaskName(), "testuser");
				}
			}
		}
		webgroupService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
